<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Department.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Pattern.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Error.php';
?>
<SCRIPT type="text/javascript" src="/view/javascripts/form.js"></SCRIPT>
<?php
if($response->isParameter('department')) {
	$department = $response->getParameter('department');
	$identity = $department->getIdentity();
	$name = $department->getName();
	$type = $department->getType();
	$head = $department->getHead();
	$parent = $department->getParent();
} else {
	$identity = null;
	$name = '';
	$type = null;
	$head = null;
	$parent = null;
}
$departments = $response->getParameter('departments');
$types = $response->getParameter('types');
$persons = $response->getParameter('persons');
?>
<DIV class="block">
	<H1>
		<A href="/post/index.html">Структурные подразделения</A> ::
<?php
if($identity !== null) {
	echo $name;
} else {
?>
		новое структурное подразделениеы
<?php
}
?>
	</H1>
	<FORM action="/department/update.html" method="post" id="edit">
<?php
if($identity !== null) {
?>
		<INPUT type="hidden" name="identity" value="<?php echo $identity;?>">
<?php
}
?>
		<TABLE>
			<TR>
				<TD><LABEL for="name">название структурного подразделения:</LABEL></TD>
				<TD>
					<INPUT type="text" id="name" name="name"
							value="<?php echo $name;?>"
							pattern="<?php echo Pattern::WORDS;?>"
							message="<?php echo Error::INCORRECT_DEPARTMENT_NAME;?>"
							maxlength="150">
				</TD>
			</TR>
			<TR>
				<TD><LABEL for="parent">родительское структурное подразделение:</LABEL></TD>
				<TD>
					<SELECT name="parent" id="parent">
						<OPTION></OPTION>
<?php
foreach($departments as $parentElement) {
	showParent($parentElement, 0, $parent, $identity);
}
?>
					</SELECT>
				</TD>
			</TR>
			<TR>
				<TD><LABEL for="type">тип структурного подразделения:</LABEL></TD>
				<TD>
					<SELECT name="type" id="type">
<?php
foreach($types as $typeElement) {
?>
						<OPTION value="<?php echo $typeElement->getIdentity();?>"
<?php
	if($type !== null && $typeElement->getIdentity() === $type->getIdentity()) {
?>
							selected="selected"
<?php
	}
?>
						>
<?php
	echo $typeElement->getName();
?>
						</OPTION>
<?php
}
?>
					</SELECT>
				</TD>
			</TR>
			<TR>
				<TD><LABEL for="head">руководитель структурного подразделения:</LABEL></TD>
				<TD>
					<SELECT name="head" id="head">
						<OPTION></OPTION>
<?php
foreach($persons as $person) {
	$posts = $person->getPosts();
	foreach($posts as $post) {
?>
						<OPTION value="<?php echo $post->getIdentity();?>"
<?php
		if($head !== null && $post->getIdentity() === $head->getIdentity()) {
?>
							selected="selected"
<?php
		}
?>
						>
<?php
		echo $person->getFullName().' ('.$post->getName().', '.$post->getRate().')';
?>
						</OPTION>
<?php
	}
}
?>
					</SELECT>
				</TD>
			</TR>
		</TABLE>
	</FORM>
</DIV>
<DIV class="block">
	<BUTTON type="button" onclick="submitForm('edit')">
		<IMG src="/view/img/button-save.png" alt="">
		<SPAN>сохранить</SPAN>
	</BUTTON>
	<BUTTON type="button" onclick="resetForm('edit')">
		<IMG src="/view/img/button-reset.png" alt="">
		<SPAN>сбросить</SPAN>
	</BUTTON>
<?php
if($identity) {
?>
	<FORM action="/department/delete.html" method="post" class="inline">
		<INPUT type="hidden" name="identity" value="<?php echo $identity;?>">
		<BUTTON type="submit">
			<IMG src="/view/img/button-delete.png" alt="">
			<SPAN>удалить</SPAN>
		</BUTTON>
	</FORM>
<?php
}
?>
	<FORM action="/department/index.html" method="get" class="inline">
		<BUTTON type="submit">
			<IMG src="/view/img/button-back.png" alt="">
			<SPAN>назад</SPAN>
		</BUTTON>
	</FORM>
</DIV>

<?php
function showParent(Department $parent, $level, $selected, $id) {
	if($parent->getIdentity() !== $id) {
?>
<OPTION value="<?php echo $parent->getIdentity();?>"
<?php
		if($selected !== null && $parent->getIdentity() === $selected->getIdentity()) {
?>
	selected="selected"
<?php
		}
?>
>
<?php
		for($i = 0; $i < $level; $i++) {
			echo '&nbsp;&nbsp;&nbsp;';
		}
		echo $parent->getName();
?>
</OPTION>
<?php
		$children = $parent->getChildren();
		foreach($children as $child) {
			showParent($child, $level + 1, $selected, $id);
		}
	}
}
?>